//
//  ShowPriceBaseListVC.swift
//  ZYA
//
//  Created by 邱仙凯 on 2024/9/20.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
 
class ShowPriceBaseListVC: TenderBaseTableController,UISearchBarDelegate {
    
    var page = 1
    
    var dataArray = [ZtbContentModel]()
    var z_zb_id = "" // 投标记录使用
    
    var btnflag = "1"
    
    var billTitle = ""
    var followTag = ""
    var api:API!
    private var searchBar:UISearchBar?
    
    
    init(z_zb_id:String = "",searchTitle:String = "",followTag:String = "0") {
        super.init(nibName: nil, bundle: nil)
        self.followTag = followTag
        self.z_zb_id = z_zb_id
        self.helper.filterPa!.searchTitle = searchTitle
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "出价记录"
        let rightbtn = UIButton.creat4Item(image: UIImage(named: "all_record")!) { _ in
            getRecordListtt(zDtradeType: "",zb_id: self.z_zb_id, staff_id: "",billtitle: self.billTitle,zb_source: "0",s_data_source: "1")

        
        }
        let right = UIBarButtonItem(customView:rightbtn)
        self.navigationItem.rightBarButtonItem = right
        helper.callTableRefreshClosure = {
            self.tableView.mj_header?.beginRefreshing()
            
        }
        
        tableView.estimatedRowHeight = 50
        tableView.rowHeight = UITableView.automaticDimension
        
        tableView.mj_header = KSRefreshHeader { [unowned self] in
            self.page = 1
            self.network()
        }
        tableView.mj_footer = KSRefreshFooter { [unowned self] in
            self.page += 1
            self.network()
        }
        tableView.mj_header?.beginRefreshing()
        
        tableView.register(ShowPriceBaseCell.self, forCellReuseIdentifier: ShowPriceBaseCell.identifier)
        
        
        
        tableView.tableFooterView = UIView()
        
        
    }
    func configAPI() {
        api = API.queryBidInfoList(page: "\(page)", size: "10", jp_id: z_zb_id, z_title: self.helper.filterPa!.searchTitle, z_sort: "1",s_data_source: "1")
         
    }
    
    @objc func doneClickss() {
        self.searchBar!.resignFirstResponder()
    }
    
 
    @objc func publishAction() {
        self.helper.publishTender(controller:self)
    }
    @objc func mapAction() {
        
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        self.helper.filterPa!.searchTitle = searchBar.text.unwrap
        self.tableView.mj_header?.beginRefreshing()
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.dataArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let one = self.dataArray[indexPath.row]
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ShowPriceBaseCell.identifier, for: indexPath) as? ShowPriceBaseCell else {
            fatalError("cell error")
        }
        
        one.innerType = .chujiaRecordKHW
        cell.makeConstrainsts()
        cell.oper_btn.addTouchUpInSideAction {[one] _ in
            let nav = BaseNavigationController()
            let vc = ZyaCarInfoVC()
            vc.dataArray = one.carInfo
            nav.viewControllers = [vc]
            self.present_FullScreen(to: nav)
            
            
        }
        
        
        cell.one = one
        cell.mychujiaBtn.addTouchUpInSideAction { [one] _ in
            
            
          
            getRecordListtt(zDtradeType: one.zDtradeType.unwrap,zb_id: one.zZbId.unwrap, staff_id: one.zStaffId.unwrap,billtitle: self.billTitle,zb_source: "0",s_data_source: "1")
                 
               
           
           
        }
         
        
        return cell
        
        
        
        
        
        
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let one = self.dataArray[indexPath.row]
        
//        return CGFloat(40 + (one.bidInfoArr?.count ?? 0)*30)
        let cell = ShowPriceBaseCell.init(style: .default, reuseIdentifier: nil)
        cell.makeConstrainsts()
        return cell.calculateHeight(one: one)
    }
    
    func network () {
     configAPI()
        KSNetwork.shared.doRequest(api,showHud: false) { [weak self] (data:BaseResult<ZtbContentModel>) in
            guard let `self` = self else { return }
            if data.flag == TRUE {
                if let infos = data.infoArrCombine {
                    if self.page == 1 {
                        self.dataArray.removeAll()
                    }
                    self.dataArray += infos
                    self.tableView.reloadData()
                    self.tableView.mj_header?.endRefreshing()
                    if infos.count < 10 {
                        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    } else {
                        self.tableView.mj_footer?.endRefreshing()
                    }
                }
            } else {
                KSToast(data.errorCombine.unwrap)
            }
        }
    }
    
    
    deinit {
        print("tenderfirst 销毁")
    }
}
